<!--
 * @Author: your name
 * @Date: 2022-01-31 15:18:29
 * @LastEditTime: 2022-03-12 16:43:47
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \vue3-ts-food-admin\src\views\seatManage\seatManage.vue
-->
<template>
  <div class="seatManageContainer">
    <div class="seatManageSection">
      <div class="seatManageSectionTitle">
        <div class="titleLeft">区域</div>
        <div class="titleRightBtns">
          <div class="rightBtnItem">
            <el-icon color="#3758ff" style="margin-right: 5px"
              ><elicon-circle-plus
            /></el-icon>
            添加区域
          </div>
          <div class="rightBtnItem">
            <el-icon color="#3758ff" style="margin-right: 5px"
              ><elicon-wallet
            /></el-icon>
            区域管理
          </div>
        </div>
      </div>
      <div class="seatManageSectionBody">
        <div
          v-for="category in state.seatCategory"
          class="areaCategoryItem"
          :class="{ selected: category.id === state.selectedId }"
          :key="category.id"
          @click="switchCategory(category.id, category.name)"
        >
          {{ category.name }}
        </div>
      </div>
    </div>
    <div class="seatManageSection">
      <div class="seatManageSectionTitle">
        <div class="titleLeft">全部</div>
        <div class="titleRightBtns">
          <div class="rightBtnItem">
            <el-icon color="#3758ff" style="margin-right: 5px"
              ><elicon-circle-plus
            /></el-icon>
            添加桌台
          </div>
          <div class="rightBtnItem">
            <el-icon color="#3758ff" style="margin-right: 5px"
              ><elicon-edit
            /></el-icon>
            桌台编辑
          </div>
        </div>
      </div>
      <div class="seatManageSectionBody">
        <div
          v-for="detail in state.seatCategoryDetailList"
          :key="detail.id"
          class="categoryDetailItem"
          :class="detail.available && 'available'"
        >
          <div class="categoryDetailTop">
            <div class="categoryName">{{ detail.categoryName }}</div>
            <div class="detailName">({{ detail.name }})</div>
          </div>
          <div class="categoryDetailBottom">
            {{ detail.available ? detail.sales : "空闲" }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, watch } from "vue";

import { SeatCategory, SeatDetailItem } from "@/types/index";
import { getSeatCategory, getSeatCategoryDetails } from "@/api/cloud";

interface SeatManageState {
  seatCategory: SeatCategory[];
  selectedId: number;
  seatCategoryDetailList: SeatDetailItem[];
  categoryName: string;
}

export default defineComponent({
  setup() {
    const state = reactive<SeatManageState>({
      seatCategory: [],
      selectedId: -1,
      seatCategoryDetailList: [],
      categoryName: "全部",
    });

    const switchCategory = (id: number, name: string) => {
      state.selectedId = id;
      state.categoryName = name;
    };
    // 查询下半部分的座位
    watch(
      () => state.selectedId,
      () => {
        getSeatCategoryDetails(state.selectedId, state.categoryName).then(
          (res) => {
            const { data } = res;
            if (data && data.code === 200) {
              state.seatCategoryDetailList = data.result;
              console.log(state);
            }
          }
        );
      }
    );

    return {
      state,
      switchCategory,
    };
  },
  mounted() {
    getSeatCategory().then((res) => {
      const { data } = res;
      if (data && data.code === 200) {
        this.state.seatCategory = data.result;
      }
    });

    getSeatCategoryDetails(-1, this.state.categoryName).then((res) => {
      const { data } = res;
      if (data && data.code === 200) {
        this.state.seatCategoryDetailList = data.result;
      }
    });
  },
});
</script>

<style lang="less" scoped>
.seatManageContainer {
  padding: 40px;
  .seatManageSection {
    border: 1px solid #ddd;
    margin-bottom: 40px;
    .seatManageSectionTitle {
      height: 60px;
      line-height: 60px;
      padding-left: 16px;

      border-bottom: 1px solid #ddd;
      display: flex;
      justify-content: space-between;
      .titleLeft {
        font-size: 16px;
        color: #666;
        height: 60px;
        line-height: 60px;
      }
      .titleRightBtns {
        height: 60px;
        color: #3758ff;
        display: flex;
        align-items: center;
        margin-right: 16px;
        .rightBtnItem {
          cursor: pointer;
          display: flex;
          align-items: center;
          height: 60px;
          margin-left: 32px;
        }
      }
    }
    .seatManageSectionBody {
      padding: 30px 20px;
      display: flex;
      flex-wrap: wrap;
    }
    .categoryDetailItem {
      width: 172px;
      border: 1px solid #ddd;
      border-radius: 4px;
      height: 182px;
      margin-right: 16px;
      .categoryDetailTop {
        height: 82px;
        display: flex;
        align-items: center;
        border-bottom: 1px dotted #ddd;
        flex-direction: column;
        justify-content: center;
        .categoryName {
          margin-bottom: 8px;
          color: #333;
          font-size: 16px;
          font-weight: bold;
        }
        .detailName {
          color: #999;
          font-size: 14px;
        }
      }
      .categoryDetailBottom {
        display: flex;
        height: 100px;
        align-items: center;
        justify-content: center;
        font-size: 18px;
        color: #aaa;
      }

      &.available {
        border-color: #3758ff;
        .categoryDetailTop {
          background-color: #3758ff;
          color: #fff;
          > div {
            color: #fff;
          }
        }
        .categoryDetailBottom {
          color: red;
          font-weight: bold;
        }
      }
    }
    .areaCategoryItem {
      height: 28px;
      padding: 0 16px;
      line-height: 28px;
      border-radius: 4px;
      border: 1px solid #ddd;
      color: #333;
      cursor: pointer;
      margin-right: 10px;
      &.selected {
        color: #fff;
        background-color: #3758ff;
      }
    }
  }
}
</style>
